package com.wuyou.designPatterns.composite;

import java.util.ArrayList;
import java.util.Iterator;

public class Directory extends Entry{

    private final String name;

    /**
     * 文件夹名字
     */
    private final ArrayList<Entry> directory = new ArrayList();

    public Directory(String name){
        this.name = name;
    }

    @Override
    public Entry add(Entry entry) throws FileTreatmentException {
        directory.add(entry);
        return this;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public int getSize() {
        int size = 0;
        Iterator it = directory.iterator();
        while (it.hasNext()){
            Entry next = (Entry)it.next();
            size += next.getSize();
        }
        return size;
    }

    /**
     * 显示目录一览
     * Java语言的特点 prefix + "/" + this 等价于 prefix + "/" + this.toString() || prefix + "/" + toString()
     * @param prefix
     */
    @Override
    public void printList(String prefix) {
        System.out.println(prefix + "/" + this);
        Iterator it = directory.iterator();
        while (it.hasNext()){
            Entry entry = (Entry)it.next();
            entry.printList(prefix + "/" + name);
        }
    }
}
